package com.lyf.boot.modules.index.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.lyf.boot.annotation.ApiVersion;
import com.lyf.boot.annotation.RateLimiter;
import com.lyf.boot.constant.RabbitMqConstant;
import com.lyf.boot.core.model.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * MQ测试接口
 * @author Luyufan
 * @date 2023/7/10 18:12
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/mq")
public class RabbitMqController {

    private final RabbitTemplate rabbitTemplate;

    /**
     * 延迟消息
     * @return void
     */
    @GetMapping("/send1")
    @SaIgnore
    public R<Void> send1(){
        log.info("发送延迟消息");
        rabbitTemplate.convertAndSend(RabbitMqConstant.DELAY_EXCHANGE, RabbitMqConstant.DELAY_ROUTING, "延迟消息", message -> {
            //单位，毫秒
            message.getMessageProperties().setDelayLong(5000L);
            return message;
        });
        return R.ok();
    }

    /**
     * 延迟消息
     * @since 2.0.0
     * @return void
     */
    @ApiVersion("2.0.0")
    @GetMapping("/{version}/send1")
    @SaIgnore
    public R<Void> sendV2(){
        log.info("发送延迟消息2.0.0");
        rabbitTemplate.convertAndSend(RabbitMqConstant.DELAY_EXCHANGE, RabbitMqConstant.DELAY_ROUTING, "延迟消息", message -> {
            //单位，毫秒
            message.getMessageProperties().setDelayLong(12000L);
            return message;
        });
        return R.ok();
    }

    /**
     * 订阅消息
     * @return void
     */
    @GetMapping("/send2")
    @SaIgnore
    public R<Void> send2(){
        log.info("发送订阅消息");
        rabbitTemplate.convertAndSend(RabbitMqConstant.PUBLISH_FANOUT_EXCHANGE, "", "订阅消息");
        return R.ok();
    }

    /**
     * 一对一消息
     * @return void
     */
    @GetMapping("/send3")
    @RateLimiter(time = 1, count = 2)
    @SaIgnore
    public R<Void> send3(){
        log.info("发送一对一消息");
        rabbitTemplate.convertAndSend(RabbitMqConstant.ONE_TO_ONE_QUEUE, "一对一消息");
        return R.ok();
    }

    /**
     * 一对一消息
     * @return void
     */
    @GetMapping("/send4")
    @SaIgnore
    public R<Void> send4(){
        log.info("发送topic消息");
        rabbitTemplate.convertAndSend(RabbitMqConstant.TOPIC_EXCHANGE,"topic_routing.a", "topic消息a");
        return R.ok();
    }

    /**
     * 一对一消息
     * @return void
     */
    @GetMapping("/send5")
    @SaIgnore
    public R<Void> send5(){
        log.info("发送topic消息");
        rabbitTemplate.convertAndSend(RabbitMqConstant.TOPIC_EXCHANGE,"topic_routing.b", "topic消息b");
        return R.ok();
    }



}
